package com.mmm.station.controller;


import com.mmm.station.entity.User;

import com.mmm.station.security.utils.ContextHolderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.concurrent.TimeUnit;

/**
 * Created by mmm on 2016/11/9.
 */
@Controller
public class IndexController {


    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @Autowired
    private ValueOperations<Object, Object> redis;


    @RequestMapping(value = "")
    public String toHello(Model model) {
//        Subject.getInstance().setManager(manager);
//        System.out.println(Subject.getInstance().getManager());
//     Manager manager= (Manager)  ContextHolderUtils.getContext().getBean("manager");


        redis.set("你好", "好呀", 1, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set("sdfasdf", new User());
        model.addAttribute("name", "你好");
        System.out.println(redis.get("你好"));
        return "hello";
    }

    @RequestMapping(value = "setHtml")
    @ResponseBody
    public String setHtml(HttpSession session, HttpServletRequest request) {
        User user = new User();
        user.setRole("bb");

        redis.set("aa", user);

        String path = this.getClass().getResource("/").getPath() + "static/aa.html";
        String strHtml = getHtmlToString(path);
        session.setAttribute("aa", strHtml);


        return "成功";
    }

    //    @Go(role = "进去了")
    @RequestMapping(value = "getHtml")
    public void getHtml(HttpSession session, HttpServletResponse response) {
        User user = (User) redis.get("aa");

        System.out.println("a-=-=-=-=");
        System.out.println(user.getRole());


//        return (String) session.getAttribute("aa");
//        response.setContentType("");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.println(ContextHolderUtils.getSession().getAttribute("aa"));
//        out.println(session.getAttribute("aa"));
//        out.println("<html><head></head><body>1111</body></html>");
        out.close();
    }


    public String getHtmlToString(String path) {
        String str = "";
        File myFile = new File(path);

        if (!myFile.exists()) {
            return null;
        }
        try {
//            BufferedReader in = new BufferedReader(new FileReader(myFile));
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(myFile), "utf-8"));


            String strTemp;
            while ((strTemp = in.readLine()) != null) {
                str += strTemp;
            }

            in.close();
        } catch (IOException e) {
            e.getStackTrace();
        }

        return str;
    }


//    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveFilename),"GB2312")));
}
